package com.eeepay.eeepay_v2.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.SaleAdvAdapter;

import java.util.List;

import cn.eeepay.api.grpc.nano.BannerProto;

/**
 *自定义广告轮播展示类
 */
public class CustomDisplayView extends RelativeLayout {
	private View view;
	private CustomViewPager disPager;//
	private RadioGroup vpRadioGroup;
	private int countPager;
	private SaleAdvAdapter adapter;

	public CustomViewPager getDisPager() {
		return disPager;
	}

	public CustomDisplayView(Context context) {
		super(context);
		InitView(context);
	}

	private void InitView(Context context) {
		view = View.inflate(getContext(), R.layout.sale_viewpager, this);
		disPager = (CustomViewPager) view.findViewById(R.id.display_viewpager);
		vpRadioGroup = (RadioGroup) view.findViewById(R.id.vp_radiogroup);
		InitListener();
	}

	/**
	 * 初始化监听器
	 */
	private void InitListener() {
		disPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				RadioButton radioButton = (RadioButton) vpRadioGroup
						.getChildAt(position);
				radioButton.setChecked(true);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		vpRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				disPager.setCurrentItem(checkedId);
			}
		});
	}

	/**
	 *  添加adapter
	 */
	public void setAdapter(SaleAdvAdapter adapter) {
		this.adapter = adapter;
		countPager = adapter.getCount();
		RadioButton radioButton;
		for (int i = 0; i < countPager; i++) {
			radioButton = new RadioButton(getContext());
			radioButton.setButtonDrawable(R.drawable.radio_selector);
			radioButton.setId(i);
			radioButton.setPadding(3, 0, 3, 0);
			vpRadioGroup.addView(radioButton);
		}
		vpRadioGroup.check(0);
		disPager.setAdapter(this.adapter);
		disPager.setIGetViewHeight(new CustomViewPager.IGetViewHeightListener() {
			@Override
			public void getViewHeight(int height) {
				if (listener != null)
					listener.getHeight(height);
			}
		});
		SwitchViewPager();
	}

	public void setDatas(List<BannerProto.BannerMsg> bannerMsgs){
		vpRadioGroup.removeAllViews();
		for (int i = 0; i < bannerMsgs.size(); i++) {
			RadioButton radioButton = new RadioButton(getContext());
			radioButton.setButtonDrawable(R.drawable.radio_selector);
			radioButton.setId(i);
			radioButton.setPadding(3, 0, 3, 0);
			vpRadioGroup.addView(radioButton);
		}
		vpRadioGroup.check(0);
		adapter.setDatas(bannerMsgs);
		countPager = adapter.getCount();
	}

	/**
	 * 页面自动轮播
	 */
	private void SwitchViewPager() {
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				Integer currentItem = (Integer) msg.obj;
				disPager.setCurrentItem(currentItem);
			}
		};
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message msg;
				int i = 0;
//				boolean toLeft = true;
				while (true) {
					msg = Message.obtain();
					msg.obj = i++;
					if (i >= countPager) {
						i = 0;
					}

					handler.sendMessage(msg);
					try {
						Thread.sleep(5000);//add by zhuangzeqin 2017年9月26日19:15:00 根据产品需求； banner图修改轮播时间为每5秒一轮换
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	private onGetViewHeightListener listener;

	public interface onGetViewHeightListener {
		void getHeight(int height);
	}

	public void setOnGetViewHeightListener(onGetViewHeightListener listener) {
		this.listener = listener;
	}
}
